﻿/// <reference name="MicrosoftAjax.js"/>

var _tblId = '';
var _department_code = '';
var _staff_code = 0;
var cObjMasterType;
var cObjMainSystem;
var cObjMenuSystem;
var cObjSubMenu;
var cAuthDepartment;
var cAuthenDeptCode;
var _saveService = "../WebService.asmx/SaveAuthenStaffObject";
var _loadService = "../WebService.asmx/GetAuthenStaffObject";


function markRow(objCheckbox) {

    var cbs = $(objCheckbox)
                .parent()
                .parent()
                .find(':input')
                .filter(':checkbox').toArray();
                
    for (idx in cbs) {

        if ($(cbs[idx]).attr('disabled') == false)
        {
            $(cbs[idx]).attr('checked', $(objCheckbox).attr('checked'));
        }
    }
            

}



function loadAuthDepartmentObject(p_department_code, p_staff_code, pnlId) {

    var p_is_edit = location.href.indexOf('Edit') > -1;
    var xdoc = $.ajax({
        url: _loadService,
        global: false,
        type: "POST",
        data: ({ department_code: p_department_code, staff_code: p_staff_code ,isEdit : p_is_edit}),
        dataType: "script",
        async: false
    }
    ).responseXML;

    //Generate table
    $('#' + pnlId).html(generateUI(xdoc.text));

}

function saveAuth() {

    var jstring = YAHOO.lang.JSON.stringify(cAuthDepartment);
    var xdoc = $.ajax({
        url: _saveService,
        global: false,
        type: "POST",
        data: ({ jdata: jstring }),
        dataType: "script",
        async: false,
        success: function() {

            alert('ข้อมูลถูกบันทึกแล้ว')

            if (location.href.indexOf('Edit') > -1) {
                window.location = 'stp_auth_staff_list.aspx?Edit=true';
            }
            else {
                window.location = 'stp_auth_staff_fv.aspx?Insert=1';
            } 

        }
    }
    )
}


function embedData(objData) {

    objData = eval('(' + objData + ')');


}

function hasAuthObject(cAuthDepartment, sub_menu_code) {

    for (idx in cAuthDepartment) {
        if (cAuthDepartment[idx].sub_menu_code == sub_menu_code) {
            return true;
        }
    }
    return false;
}

function isEnable(cAuthDepartment, sub_menu_code) {
    for (idx in cAuthDepartment) {
        if (    cAuthDepartment[idx].sub_menu_code == sub_menu_code
            && cAuthDepartment[idx].auth_staff_code != 0
            ) 
        {

            return true;
        }
    }
    return false;
}

//call service
function generateUI(objData) {

    objData = eval('(' + objData + ')');

    var strUi = '';
    var countColumn = 0;

    //Store data to client.
    cObjMasterType = objData.MasterTypes;
    cObjMainSystem = objData.MainSystems;
    cObjMenuSystem = objData.MenuSystems;
    cObjSubMenu = objData.SubMenus;
    cAuthDepartment = objData.AuthDepartments;
    cAuthenDeptCode = objData.AuthenDeptCodes;

    _department_code = objData.Department_code;
    _staff_code = objData.Staff_code;

    strUi += '<table id="' + _tblId + '"  cellspacing="2" cellpadding="2" bordercolor="#999999" border="0" align="center" width="100%">';


    //Generate Header
    strUi += '<tbody><tr bgcolor="#ff9999" align="center"  class="component_caption"><td  class="th_data" colspan="2">ระบบ/เมนูระบบ</td>';
    $.map(cObjMasterType, function(n, i) {

        strUi += String.format('<td class="th_data">{0}</td>', n.type_name);
        countColumn++;
    })
    strUi += '</tr>';


    //Generate Body
    for (main in cObjMainSystem) {

        //Main system
        strUi += '<tr>';
        strUi += String.format('<td bgcolor="#D8D8D8"  colspan="2">{0}</td>', cObjMainSystem[main].main_system_name)
        for (iLoop = 0; iLoop < countColumn; iLoop++) {
            strUi += String.format('<td bgcolor="#D8D8D8" width="80px">&nbsp;</td>');
        }
        strUi += '</tr>';

        //Menu System
        for (menu in cObjMenuSystem) {

            if (cObjMainSystem[main].main_system_code == cObjMenuSystem[menu].main_system_code) {

                //Create menu label
                strUi += '<tr>';
                strUi += String.format("<td><input type='checkbox' onclick='markRow(this)' /></td>");
                strUi += String.format("<td>{0}</td>", cObjMenuSystem[menu].menu_system_name);

                for (mtype in cObjMasterType) {

                    strUi += "<td align='center' class='td_data' bordercolor='#CCCCCC'>";

                    for (sub in cObjSubMenu) {

                        if (cObjSubMenu[sub].menu_system_code == cObjMenuSystem[menu].menu_system_code) {
                            if (cObjSubMenu[sub].type_code == cObjMasterType[mtype].type_code) {
                                var hasAuth = hasAuthObject(cAuthDepartment, cObjSubMenu[sub].sub_menu_code);
                                var canShow = isEnable(cAuthDepartment, cObjSubMenu[sub].sub_menu_code);
                                var cbData = String.format(
                                '<input class="cb_data" {1} onclick="updateAuthObject({0},this)" type="checkbox" {2} />',
                                cObjSubMenu[sub].sub_menu_code,
                                //hasAuth == true ? '' : 'disabled="disabled',
                                hasAuth == true ? '' : 'style="display:none;"',
                                canShow == true ? "checked='checked'" : ""
                            );
                                strUi += String.format("{0}", cbData);
                            }

                        }

                    };

                    strUi += "&nbsp;</td>";

                }

                strUi += '</tr>';
            }

        }

    };

    strUi += String.format(
                    '<tr><td align="center" bgcolor="#ffffff" class="td_data_end col_delete" colspan="{0}"><center><button onclick="saveAuth()" type="button">บันทึกข้อมูล</button><button type="reset">ยกเลิก</button></center></td></tr>',
                    countColumn + 2);
    strUi += "</tbody></table>"



    //jObjMasterType, jObjContent
    /*
    {type_code,type_name}  master_tyee
    {main_system_code,main_system_name,department_code}
    {sub_menu_code,type_code,}
    */

    return strUi;
}





function removeAuthObject(cAuthDepartment, sub_menu_code) {
    for (idx in cAuthDepartment) {
        if (cAuthDepartment[idx].sub_menu_code == sub_menu_code) {
            Array.removeAt(cAuthDepartment, idx);
        }
    }
}

function updateAuthObject(sub_menu_code, objCheckbox) {


    for (idx in cAuthDepartment) {

        if (cAuthDepartment[idx].sub_menu_code == sub_menu_code) {

            if ($(objCheckbox).attr('checked') == true) {
                cAuthDepartment[idx].staff_code = _staff_code;
                cAuthDepartment[idx].auth_dep_code = cAuthDepartment[idx].auth_department_code;
            }
            else {
                cAuthDepartment[idx].staff_code = 0;
                cAuthDepartment[idx].auth_dep_code = 0;
            }
        }
        

    }
        

}


